package com.novellotus.iov.carvice.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.activity.MainActivity;
import com.novellotus.iov.carvice.adapter.WorkFragmentAdapter;
import com.novellotus.iov.carvice.bean.RxEinfos;
import com.novellotus.iov.carvice.utils.MyViewUtils;

import org.angmarch.views.NiceSpinner;

import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class WorkFragment extends BaseFragment {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.nice_spinner)
    NiceSpinner nice_spinner;
    @BindView(R.id.tv_tittle)
    TextView tv_tittle;
    @BindView(R.id.rlt_no_enterprise)
    RelativeLayout rlt_no_enterprise;
    private List<RxEinfos> einfosList;
    private int COMPANY_POSITION = 0;
    private WorkFragmentAdapter workFragmentAdapter;


    public WorkFragment() {
        // Required empty public constructor
    }


    @Override
    View initRootView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_work, container, false);
    }

    @Override
    void init() {
        recyclerView.setLayoutManager(MyViewUtils.getLinearLayoutManager(getContext()));
        // getList();
    }

    /**
     * 获取角色列表
     */
    public void setEnterprise(List<RxEinfos> list) {
        einfosList = list;

        if (list.size() != 0) {
            workFragmentAdapter = new WorkFragmentAdapter(getContext(), list.get(0));
            recyclerView.setAdapter(workFragmentAdapter);
            initSpinner();
            tv_tittle.setVisibility(View.GONE);
            nice_spinner.setVisibility(View.VISIBLE);
            rlt_no_enterprise.setVisibility(View.GONE);
        } else {
            rlt_no_enterprise.setVisibility(View.VISIBLE);
            tv_tittle.setVisibility(View.VISIBLE);
            nice_spinner.setVisibility(View.GONE);
        }
    }

    private void initSpinner() {
        List<String> dataset = new LinkedList<>();
        for (RxEinfos rxEinfos : einfosList) {
            dataset.add(rxEinfos.getEname());
        }
        if (dataset.size() == 1) {
            nice_spinner.hideArrow();
        } else {
            nice_spinner.showArrow();
        }
        nice_spinner.attachDataSource(dataset);
        nice_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if (i != COMPANY_POSITION) {
                    COMPANY_POSITION = i;
                    workFragmentAdapter.updateCompany(einfosList.get(i));
                    mainActivity.updateMineFragment(einfosList.get(i), i);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }


        });
        if (einfosList.size() >= 5)
            nice_spinner.setMaxPopupWindowHeight((int) getResources().getDimension(R.dimen.spinner_max_height));
    }

    public void updateEnterprise(RxEinfos rxEinfos, int position) {
        workFragmentAdapter = new WorkFragmentAdapter(getContext(), rxEinfos);
        recyclerView.setAdapter(workFragmentAdapter);
        nice_spinner.setSelectedIndex(position);
        COMPANY_POSITION = position;
    }

    MainActivity mainActivity;

    public void setActivity(MainActivity mainActivity) {
        this.mainActivity = mainActivity;
    }

}
